package com.aco.party.util;

import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.FileInputStream;
import java.util.Iterator;

/**
 * Created by flying on 6/26/2016 0026.
 */
public class CMYKImageUtil{
    public static BufferedImage convert(FileInputStream inputStream,String filePath) throws Exception {
        // 找一个reader
        Iterator readers = ImageIO.getImageReadersByFormatName("JPEG");
        ImageReader reader = null;
        while (readers.hasNext()) {
            reader = (ImageReader) readers.next();
            if (reader.canReadRaster()) {
                break;
            }
        }

        // 设置input.
        ImageInputStream input = ImageIO.createImageInputStream(inputStream);
        reader.setInput(input);

        // 创建图片.
        BufferedImage image;
        try {
            // 尝试读取图片 (包括颜色的转换).
            image = reader.read(0);
        } catch (IIOException e) {
            // 读取Raster (没有颜色的转换).
            Raster raster = reader.readRaster(0, null);

            // 随意选择一个BufferedImage类型.
            int imageType;
            switch (raster.getNumBands()) {
                case 1:
                    imageType = BufferedImage.TYPE_BYTE_GRAY;
                    break;
                case 3:
                    imageType = BufferedImage.TYPE_3BYTE_BGR;
                    break;
                case 4:
                    imageType = BufferedImage.TYPE_4BYTE_ABGR;
                    break;
                default:
                    throw new UnsupportedOperationException();
            }

            // 创建一个BufferedImage.
            image = new BufferedImage(raster.getWidth(), raster.getHeight(), imageType);

            // 设置图片数据.
            image.getRaster().setRect(raster);
        }
        return image;
    }
}
